package com.example.ispy;

import java.util.HashMap;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnTouchListener{
	
	int pageNo;	
	
	TouchImageView background;
	Integer toFind;
	int hitBoxID;
	
	ImageButton blackBtn;
	ImageButton redBtn;
	ImageButton blueBtn;
	ImageButton nextBtn;
	ImageButton prevBtn;
	
	public static HashMap<Integer,HashMap<String,Integer>> idMap = resourceIdMap();
	
	boolean isCleared = false;
	boolean foundBlack = false;	
	boolean foundRed = false;
	boolean foundBlue = false;
	public static final String PASSED = "passed";
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		pageNo = intent.getIntExtra("pageNo", 0);
        toFind= (Integer) idMap.get(pageNo).get("toFind");
        hitBoxID = (Integer) idMap.get(pageNo).get("hitBox");
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public void onStart(){
    	super.onStart();
        setContentView((Integer) idMap.get(pageNo).get("layout"));    	
        background = (TouchImageView)findViewById((Integer) idMap.get(pageNo).get("background"));        
        background.setOnTouchListener(this);
        
        blackBtn = (ImageButton)findViewById((Integer) idMap.get(pageNo).get("blackBtn"));
        
        if(!isCleared){
            blackBtn.setAlpha(0.5f);	
        } else {
        	blackBtn.setAlpha(1f);
        }
        

        try{
        	redBtn = (ImageButton)findViewById((Integer) idMap.get(pageNo).get("redBtn"));
            if(!isCleared){
                redBtn.setAlpha(0.5f);	
            } else {
            	redBtn.setAlpha(1f);
            }

        } catch (Exception e) {
        	//do nothing because there is no red button
        }
        
        try {
            blueBtn = (ImageButton)findViewById((Integer) idMap.get(pageNo).get("blueBtn"));
            if(!isCleared){
                blueBtn.setAlpha(0.5f);	
            } else {
            	blueBtn.setAlpha(1f);
            }
        } catch (Exception e) {
        	//do nothing because there is no blue button.
        }
                
        prevBtn = (ImageButton)findViewById((Integer) idMap.get(pageNo).get("prevBtn"));
        
        nextBtn = (ImageButton)findViewById((Integer) idMap.get(pageNo).get("nextBtn"));
        
        if(isCleared){
            nextBtn.setClickable(true);
            nextBtn.getBackground().setAlpha(255);
        } else {
            nextBtn.getBackground().setAlpha(125);
            nextBtn.setClickable(false);
        }
    }
    
    @TargetApi(16)
	@Override
    public void onStop(){
    	super.onStop();
    	
    	background.setOnTouchListener(null);
    	background.setBackground(null);  
    	background.setBackgroundResource(0);
    	
    	background.mScaleDetector = null;
    	background.setOnTouchListener(null);
    	
    	background.b.recycle();
    	background.b = null;
    	background.destroyDrawingCache();
    	
    	
    	background.hb = null;
    	
    	background.hitbox.recycle();
    	background.hitbox = null;
    	background.hitDraw = null;
    	background = null;
    	
    	blackBtn.destroyDrawingCache();
    	blackBtn.setBackground(null);
    	blackBtn.setBackgroundResource(0);
    	blackBtn = null;
    	
    	prevBtn.destroyDrawingCache();
    	prevBtn.setBackground(null);
    	prevBtn.setBackgroundResource(0);
    	prevBtn = null;
    	
    	nextBtn.destroyDrawingCache();
    	nextBtn.setBackground(null);
    	nextBtn.setBackgroundResource(0);
    	nextBtn = null;

    	System.gc();
    }

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		
		if(event.getAction()==0){
						
			int pixel = background.b.getPixel((int)event.getX(), (int)event.getY());
						
			if(pixel==Color.BLACK && !foundBlack){
				blackBtn.setAlpha(1f);
				toFind -=1;
				foundBlack = true;
				playSound();
				if(toFind.equals(0)){
					showDialog();
				}
			}
				
			if(pixel==Color.RED && !foundRed) {
				redBtn.setAlpha(1f);
				toFind -=1;
				foundRed = true;
				playSound();
				if(toFind.equals(0)){
					showDialog();
				}				
			}
			
			if(pixel==Color.BLUE && !foundBlue) {
				blueBtn.setAlpha(1f);
				toFind -=1;
				foundBlue = true;
				playSound();
				if(toFind.equals(0)){
					showDialog();
				}				
			}			
		}
		
		return false;	
	}
	
	public void showDialog()
    {
		//First set nextButton
		isCleared = true;
		nextBtn.getBackground().setAlpha(255);
		nextBtn.setClickable(true);
		
        android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();

        AlertDialogFragment adf = AlertDialogFragment.newInstance("You found everything!");

        adf.show(ft, "It's alive");
    }
	
	public void DialogDone(View view) {
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("pageNo", pageNo+1);
		startActivity(intent);
	}
	
	public void goBack(View view) {
		finish();
	}
	
	public void playSound() {
		MediaPlayer mp = MediaPlayer.create(MainActivity.this,R.raw.found);
		mp.setVolume(100, 100);
		mp.setOnCompletionListener(new OnCompletionListener(){

			@Override
			public void onCompletion(MediaPlayer mp) {
				mp.release();			
			}
			
		});
		mp.start();
		mp = null;
	}
	
	@SuppressLint("UseSparseArrays")
	public static HashMap<Integer,HashMap<String,Integer>> resourceIdMap(){
		
		HashMap <Integer,HashMap<String,Integer>> iDmap = new HashMap<Integer,HashMap<String,Integer>>();
		
		HashMap <String,Integer> pageOne = new HashMap <String,Integer>();
		pageOne.put("layout", R.layout.activity_main);
		pageOne.put("background", R.id.background1);
		pageOne.put("hitBox", R.drawable.anthit);
		pageOne.put("nextBtn", R.id.nextBtn1);
		pageOne.put("prevBtn", R.id.prevBtn1);
		pageOne.put("blackBtn", R.id.blackBtn1);
		pageOne.put("redBtn", null);
		pageOne.put("blueBtn", null);
		pageOne.put("toFind", 1);
		
		HashMap <String,Integer> pageTwo = new HashMap <String,Integer>();
		pageTwo.put("layout", R.layout.page2);
		pageTwo.put("background", R.id.background2);
		pageTwo.put("hitBox", R.drawable.bushit);
		pageTwo.put("nextBtn", R.id.nextBtn2);
		pageTwo.put("prevBtn", R.id.prevBtn2);
		pageTwo.put("blackBtn", R.id.blackBtn2);
		pageTwo.put("redBtn", null);
		pageTwo.put("blueBtn", null);
		pageTwo.put("toFind", 1);
		
		HashMap <String,Integer> pageThree = new HashMap <String,Integer>();
		pageThree.put("layout", R.layout.activity_page3);
		pageThree.put("background", R.id.background3);
		pageThree.put("hitBox", R.drawable.cathit);
		pageThree.put("nextBtn", R.id.nextBtn3);
		pageThree.put("prevBtn", R.id.prevBtn3);
		pageThree.put("blackBtn", R.id.blackBtn3);
		pageThree.put("redBtn", null);
		pageThree.put("blueBtn", null);
		pageThree.put("toFind", 1);
		
		HashMap <String,Integer> pageFour = new HashMap <String,Integer>();
		pageFour.put("layout", R.layout.page4);
		pageFour.put("background", R.id.background4);
		pageFour.put("hitBox", R.drawable.doghit);
		pageFour.put("nextBtn", R.id.nextBtn4);
		pageFour.put("prevBtn", R.id.prevBtn4);
		pageFour.put("blackBtn", R.id.blackBtn4);
		pageFour.put("redBtn", null);
		pageFour.put("blueBtn", null);
		pageFour.put("toFind", 1);
		
		HashMap <String,Integer> pageFive = new HashMap <String,Integer>();
		pageFive.put("layout", R.layout.page5);
		pageFive.put("background", R.id.background5);
		pageFive.put("hitBox", R.drawable.egghit);
		pageFive.put("nextBtn", R.id.nextBtn5);
		pageFive.put("prevBtn", R.id.prevBtn5);
		pageFive.put("blackBtn", R.id.blackBtn5);
		pageFive.put("redBtn", null);
		pageFive.put("blueBtn", null);
		pageFive.put("toFind", 1);
		
		HashMap <String,Integer> pageSix = new HashMap <String,Integer>();
		pageSix.put("layout", R.layout.page6);
		pageSix.put("background", R.id.background6);
		pageSix.put("hitBox", R.drawable.page6hit);
		pageSix.put("nextBtn", R.id.nextBtn6);
		pageSix.put("prevBtn", R.id.prevBtn6);
		pageSix.put("blackBtn", R.id.blackBtn6);
		pageSix.put("redBtn", R.id.redBtn6);
		pageSix.put("blueBtn", null);
		pageSix.put("toFind", 2);

		HashMap <String,Integer> pageSeven = new HashMap <String,Integer>();
		pageSeven.put("layout", R.layout.page7);
		pageSeven.put("background", R.id.background7);
		pageSeven.put("hitBox", R.drawable.page7hit);
		pageSeven.put("nextBtn", R.id.nextBtn7);
		pageSeven.put("prevBtn", R.id.prevBtn7);
		pageSeven.put("blackBtn", R.id.blackBtn7);
		pageSeven.put("redBtn", R.id.redBtn7);
		pageSeven.put("blueBtn", null);
		pageSeven.put("toFind", 2);
		
		HashMap <String,Integer> pageEight = new HashMap <String,Integer>();
		pageEight.put("layout", R.layout.page8);
		pageEight.put("background", R.id.background8);
		pageEight.put("hitBox", R.drawable.page8hit);
		pageEight.put("nextBtn", R.id.nextBtn8);
		pageEight.put("prevBtn", R.id.prevBtn8);
		pageEight.put("blackBtn", R.id.blackBtn8);
		pageEight.put("redBtn", R.id.redBtn8);
		pageEight.put("blueBtn", null);
		pageEight.put("toFind", 2);
		
		HashMap <String,Integer> pageNine = new HashMap <String,Integer>();
		pageNine.put("layout", R.layout.page9);
		pageNine.put("background", R.id.background9);
		pageNine.put("hitBox", R.drawable.page9hit);
		pageNine.put("nextBtn", R.id.nextBtn9);
		pageNine.put("prevBtn", R.id.prevBtn9);
		pageNine.put("blackBtn", R.id.blackBtn9);
		pageNine.put("redBtn", R.id.redBtn9);
		pageNine.put("blueBtn", R.id.blueBtn9);
		pageNine.put("toFind", 3);
				
		HashMap <String,Integer> pageTen = new HashMap <String,Integer>();
		pageTen.put("layout", R.layout.page10);
		pageTen.put("background", R.id.background10);
		pageTen.put("hitBox", R.drawable.page10hit);
		pageTen.put("nextBtn", R.id.nextBtn10);
		pageTen.put("prevBtn", R.id.prevBtn10);
		pageTen.put("blackBtn", R.id.blackBtn10);
		pageTen.put("redBtn", R.id.redBtn10);
		pageTen.put("blueBtn", R.id.blueBtn10);
		pageTen.put("toFind", 3);
		
		HashMap <String,Integer> pageEleven = new HashMap <String,Integer>();
		pageEleven.put("layout", R.layout.page11);
		pageEleven.put("background", R.id.background11);
		pageEleven.put("hitBox", R.drawable.page11hit);
		pageEleven.put("nextBtn", R.id.nextBtn11);
		pageEleven.put("prevBtn", R.id.prevBtn11);
		pageEleven.put("blackBtn", R.id.blackBtn11);
		pageEleven.put("redBtn", R.id.redBtn11);
		pageEleven.put("blueBtn", R.id.blueBtn11);
		pageEleven.put("toFind", 3);
		
		iDmap.put(1, pageOne);
		iDmap.put(2, pageTwo);
		iDmap.put(3, pageThree);
		iDmap.put(4, pageFour);
		iDmap.put(5, pageFive);
		iDmap.put(6, pageSix);
		iDmap.put(7, pageSeven);
		iDmap.put(8, pageEight);
		iDmap.put(9, pageNine);
		iDmap.put(10, pageTen);
		iDmap.put(11, pageEleven);
		
		return iDmap;
	}		
}